/**
 * @author jprieur
 * @created April, 23 2007
 */
package mygui;

import java.util.*;
import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.*;

public class ResultDocument
{
    private String _name;
    private HashMap<String,ResultObject> _objects;

    /**
     * Return the name of the document
     * @return the name of the document as a string
     */
    public String getName() { return _name; }

    /**
     * Return the document objects
     * @return an arraylist of objects
     */
    public HashMap<String,ResultObject> getObjects() { return _objects; }

    public ResultDocument(File file)
    {
	_name = file.getName();
	_objects = new HashMap<String,ResultObject>();

	// TODO :check a DTD
	SAXParserFactory factory = SAXParserFactory.newInstance();
	// factory.setValidating(true); // DTD check

	try{
	    SAXParser parser = factory.newSAXParser();
	    parser.parse(file, new ResultContentHandler(_objects));

	}catch(FileNotFoundException fnfe){
	    System.out.println(fnfe.getMessage());
	} catch (SAXParseException spe) {
           // Error generated by the parser
	    System.out.println("\n** Parsing error"
			       + ", line " + spe.getLineNumber()
			       + ", uri " + spe.getSystemId());
	    System.out.println("   " + spe.getMessage() );
	    // Use the contained exception, if any
	    Exception  x = spe;
	    if (spe.getException() != null)
		x = spe.getException();
	    
	    // stacktrace may be too much information as a XML parsing
	    // error...
	    //	    x.printStackTrace();

        } catch (SAXException sxe) {
	    // Error generated by this application
	    // (or a parser-initialization error)
	    Exception  x = sxe;
	    if (sxe.getException() != null)
		x = sxe.getException();
	    x.printStackTrace();
        } catch (ParserConfigurationException pce) {
            // Parser with specified options can't be built
            pce.printStackTrace();
        } catch (IOException ioe) {
	    // I/O error
	    ioe.printStackTrace();
        }
    }
}
